<?php
	//timer pengecekan load page...
	include("inc/timer.php");
	$_start_timer = get_microtime();
	 
	//check protect1 jika session da tidak valid, balikin ke index.php
	//jika valid y biarin saja
	
	require_once("protect1.php");
	require_once("inc/connect.php");
	require_once("inc/settings.php");
	require_once("inc/function.php");
		
	// $pilih=mysql_select_db("uwika_krs",$conn);
	//$pdo->query("USE uwika_krs");

	//pindahkan ke lokal variable
	$_nrp = $reg_uname;
	$_semester = $_global_settings['semester']->value;
	$_tahun_ajaran = $_global_settings['tahun_ajaran']->value;						
		
	$sql = "SELECT nama FROM biodata WHERE nrp = :reg_uname ";
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array(':reg_uname' => $_nrp )) or die("ERROR: " . implode(":", $stmt->errorInfo())); //reg jurusan dari protect1.php
	$_data_mahasiswa = $stmt->fetch(PDO::FETCH_OBJ);
	
	
	//bisa diganti dengan mengambil $reg_level
	$masa_blokir = true;
	/* cek level mahasiswa */
	$sqlstr_checking_level_blokir = "SELECT username, password, level FROM security WHERE username=:uname";
	$stmt_checking_level_blokir = $pdo->prepare($sqlstr_checking_level_blokir);
	$stmt_checking_level_blokir->execute(array(':uname' => $_nrp));
	//because of php 5.2 $_POST['name'] = $name, that makes me difficult for working -_-
	if($row_checking_level_blokir = $stmt_checking_level_blokir->fetch(PDO::FETCH_OBJ)){
		if($row_checking_level_blokir->level != 1){
			$masa_blokir = true;
		}else{
			$masa_blokir = false;
		}
	}
	/* finish */
	
	/*
	 * cek validasi masih 0, jika 1 maka lagi di cek oleh dosen / tidak boleh mengambil MK kecuali Dosen Walinya / Kaprodi
	*/
	$sql = "SELECT meta_key, meta_value FROM user_metadata
			WHERE username = :username and meta_key = :meta_key 
			";
	$stmt_validasi = $pdo->prepare($sql);
	$stmt_validasi->execute(array(':username' => $_nrp
							,':meta_key' => 'validasi'));
	$_data_validasi = $stmt_validasi->fetch(PDO::FETCH_OBJ);
	if( ($_data_validasi->meta_value == 1) || ($_data_validasi->meta_value == 2)  || ($_data_validasi->meta_value == 3) ){ // 1. sudah di stujui mahasiswa, 2. sudah d stujui dosen, 3. ?
		//gagal, karna tidak ad MK tersebut untuk DIA
		$_validasi = $_data_validasi->meta_value;
	}else{
		//sukses, karna tidak ad MK tersebut untuk DIA
		$_validasi = 0;
	}
	/*
	 * END Cek Validasi KRS
	*/
	
	
	//get jurusan & dosen wali
	require_once("inc/function-db.php");
	
	$_jurusan = $function_db->getJurusan(substr($_nrp,0,3),$pdo);
	$_dosen_wali = $function_db->getDosenWali($_nrp,$pdo);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<html>
<head>
<title>Student Home !</title>
<link rel="stylesheet" type="text/css" href="css/reset_main.css">
<link rel="stylesheet" type="text/css" href="css/main.css">
<style type="text/css">
	body{ behavior:url(cssHoverFix.htc);}
	#div a { normal state }
	#div a:hover { alternative state }
</style>
</head>
<body>	
	<header id="main">
		<table id="header">
			<tr>
				<td class="header_logo_small"><a class="header_logo_small" href="#"></a></td>
				<td class="header_announce">
					<marquee style="letter-spacing:1px;marquee-speed:slow;">
						Nilai <strong>UTS</strong> Semester Gasal Periode 2012-2013 sudah keluar *sebagian. 
						Jika anda menemukan Bug / Kritik / Saran, silahkan Laporkan kepada ICT Sesegera mungkin. 
						Terima Kasih.
					</marquee>
				</td>
				<td class="header_user_name"><a class="header_user_name_a" href="#"><?php echo (strlen($_data_mahasiswa->nama) < 15 ? ucwords(strtolower($_data_mahasiswa->nama)) : ucwords(strtolower(substr($_data_mahasiswa->nama,0,15)))."..."); ?></a></td>
				<td class="header_user_photo">
					<a class="header_user_photo_a" href="#">
						<img class="header_user_photo_img" src="images/people/default.gif">
					</a>
				</td>
			</tr>
		</table>
	</header>
	<table class="board">
		<tr>
			<td class="sidebar">
				<div id="navigation">
					<ul class="top-level">
						<?php
							$_level_settings = getLevelSettings($reg_level, $pdo);
							$basename_url = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
							if(count($_level_settings->menu) > 0){
								foreach($_level_settings->menu as $foreach){
									if ($basename_url == $foreach->url){$clicked = 'class="clicked"';}else{$clicked = '';}
									echo '<li><a href="'.$foreach->url.'" '.$clicked.'>'.$foreach->nama.'</a></li>';
								}
							}else{ echo '<li><a href="#" class="clicked">Home</a></li>'; }
						?>
					</ul>
				</div>
			</td>
			<td class="bigbox">
				<div class="banner"></div>
				<div class="content letter-spacing">
					<!-- <div class="warning red">
						<label>Peringatan !</label>
						Anda belum menvalidasi <span class="sign border red">KRS</span> anda untuk dicek kepada <code>Dosen Wali</code> anda masing-masing.
					</div>
					<div class="warning red">
						<label>Peringatan !</label>
						Masa pengambilan <span class="sign border red">KRS</span> telah berakhir. Silahkan hubungi <code>BAA</code> untuk keperluan Administrasi lainnya.
					</div>
					<div class="warning green">
						<label>Sukses !</label>
						Terima kasih, anda telah menvalidasi <span class="sign border red">KRS</span>, 
						silahkan mencetak <span class="sign border red">KRS</span> anda di halaman <span class="sign border blue"><a href="">KRS Terdaftar</a></span>
						dan hubungi <code>Dosen Wali</code> anda untuk lebih lanjut.
					</div> -->
					<br \>
					<h1>KRS Anda yang Terdaftar</h1><br \>	
					<h2>Berikut dibawah ini adalah daftar mata kuliah yang sudah anda daftarkan terakhir kali</h2>
					<div class="warning blue">
						<label>Ingat !</label>
						Untuk <span class="sign border red">Membatalkan</span>, Silahkan memilih <span class="sign blue border">Mata kuliah</span> dibawah ini. 
						Jangan lupa untuk menverifikasikan serta mendiskusikannya kepada <code>Dosen Wali</code> anda masing - masing
					</div><br \>	
					<div align="center">
						<table class="data head">
							<tr>
								<td width="15%">Periode</td><td width="2%">:</td><td width="35%"><?php echo $_tahun_ajaran; ?></td><td width="15%">NRP</td><td width="2%">:</td><td width="35%"><?php echo $_nrp; ?></td>
							</tr>
							<tr>
								<td>Semester</td><td>:</td><td><?php echo $_semester; ?></td><td>Nama</td><td>:</td><td><?php echo $_data_mahasiswa->nama; ?></td>
							</tr>
							<tr>
								<td>Program Studi</td><td>:</td><td><?php echo strtoupper($_jurusan->nama_jurusan); ?></td><td>Dosen Wali</td><td>:</td><td><?php echo $_dosen_wali->nama; ?></td>
							</tr>
							<tr style="vertical-align:middle;">
								<td>Aksi Anda</td><td>:</td>	
								<td colspan="4">
									<table><tr>
										<?php 
											switch($_validasi){
												case 0 :{
											?>
											<td class="pointer" style="padding-right:5px;">
												<form name="frmsubmitkrs" method="post" action="">
													<input type="hidden" name="kodemk" value="<?php echo $_data_matakuliah[0]->kodemk; ?>">
													<input name="Submit" type="submit" value="Submit Validasi KRS Sekarang">
												</form>
											</td>
											<?php 
													break;
												}
												case 1 :{ //masa validasi
											?>
											<td style="padding-right:5px;">
												<span class="black sign border">KRS Sedang divalidasi Dosen Wali</span>
											</td>
											<?php 												
													break;
												}
												case 2 :{
												}
												default :{ //sama dengan 2 ws..
											?>
											<td style="padding-right:5px;">
												<span class="green sign border">KRS Telah Tervalidasi / Valid</span>
											</td>
											<?php 												
												}
												
											}
										?>
									</tr></table>
								</td>
								<td></td>
							</tr>
						</table><br \>
						<table class="data body">
							<thead>
								<th width="4%">No</th><th width="8%">Kode</th><th>Mata Kuliah</th><th>Status</th><th width="7%">SKS</th><th>Act!</th>
							</thead>
							<tbody>
								<?php
								$sql = "SELECT registrasi.kodemk, mk.nama, mk.sks, registrasi.sesi, registrasi.status, registrasi.validasi, registrasi.hari, 
										registrasi.mulaipukul, registrasi.selesaipukul 
										FROM registrasi, mk where sesi=:select_sesi and periode=:select_tahun_ajaran and nrp=:reg_uname and registrasi.kodemk = mk.kodemk";
								$stmt = $pdo->prepare($sql);
								$stmt->execute(array(':select_sesi' => $_semester
														,':select_tahun_ajaran' => $_tahun_ajaran
														,':reg_uname' => $_nrp )); //reg jurusan dari protect1.php
								$count = 1; //using odd php for css2 browser and less
								$jmlsks = 0;
								while($_data = $stmt->fetch(PDO::FETCH_OBJ)){ //fetch data to stdclass object
										if (empty($_data->kodemk)) {
											break;
										}
										$sql2="select hari, mulaipukul, selesaipukul FROM penawaran where kodemk=:kodemk and jurusan=:reg_jurusan";
										$stmt2 = $pdo->prepare($sql2);
										$stmt2->execute(array(':kodemk' => $_data->kodemk
															,':reg_jurusan' => $reg_jurusan )); //reg jurusan dari protect1.php
										$_data2 = $stmt2->fetch(PDO::FETCH_OBJ);
										//_data -> lama
										//_data2 -> baru
										//gtw buat apa,.. ngikut aja.. kyknya update.. pdhl setelah dipindah ke riris data registrasi ttng waktu , tidak dibutuhkan
										/*
										if($_data->hari != $_data2->hari ){
											$sql_update = "UPDATE registrasi SET hari=:haribaru WHERE nrp=:reg_uname AND kodemk=:kodemk";
											$stmt_update = $pdo->prepare($sql_update);
											$stmt_update->execute(array(':haribaru' => $_data2->hari
																,':reg_uname' => $_nrp
																,':kodemk' => $_data->kodemk ));
										}	
										if (($_data->hari == $_data2->hari) && ($_data->mulaipukul != $_data2->mulaipukul)) {
											$sql_update = "UPDATE registrasi set mulaipukul=:mulaibaru where nrp=:reg_uname and kodemk=:kodemk";
											$stmt_update = $pdo->prepare($sql_update);
											$stmt_update->execute(array(':mulaibaru' => $_data2->mulaipukul
																,':reg_uname' => $_nrp
																,':kodemk' => $_data->kodemk ));
										}
										if (($_data->hari == $_data2->hari) && ($_data->selesaipukul != $_data2->selesaipukul)) {
											$sql_update = "update registrasi set selesaipukul=:selesaibaru where nrp=:reg_uname and kodemk=:kodemk";
											$stmt_update = $pdo->prepare($sql_update);
											$stmt_update->execute(array(':selesaibaru' => $_data2->selesaipukul
																,':reg_uname' => $_nrp
																,':kodemk' => $_data->kodemk ));
										}
										*/
										//adding ahref for not supported javascript
										if($masa_blokir){
											$_data->url = "#";
										}else{
											$_data->url = "view_mk.php?kodemk=".$_data->kodemk;
										}
									?>
										<tr onclick="document.location.href='<?php echo $_data->url;?>';" <?php echo ( ($count % 2) == 0 ? 'class="odd"' : ''); ?>>
											<td class="center"><a id="block" href="<?php echo $_data->url;?>"><?php echo $count; ?></a></td>
											<td class="center"><a id="block" href="<?php echo $_data->url;?>"><?php echo $_data->kodemk; ?></a></td>
											<td align="left"><a id="block" href="<?php echo $_data->url;?>"><?php echo $_data->nama; ?></a></td>
											<td class="center"><a id="block" href="<?php echo $_data->url;?>"><?php echo $_data->status; ?></a></td>
											<td class="center"><a id="block" href="<?php echo $_data->url;?>"><?php echo $_data->sks; ?></a></td>
											<td></td>
										</tr>
									<?php
									$jmlsks += $_data->sks;
									$count++;
								}
								?>
							</tbody>
							<tfoot>
								<tr>
									<td></td><td></td><td></td><td>TOTAL</td><td><span class="sign black big border"><?php echo $jmlsks; ?></span></td><td></td>
								</tr>
							</tfoot>
						</table>
					</div>
					<br />
					<?php

					$sql = "select ips, toleransi from ips where nrp = :reg_uname";
					$stmt = $pdo->prepare($sql);
					$stmt->execute(array(':reg_uname' => $_nrp )) or die("ERROR: " . implode(":", $stmt->errorInfo())); //reg jurusan dari protect1.php
					$_data = $stmt->fetch(PDO::FETCH_OBJ);
					//list($sks,$ips,$toleransi) = $row;
					
					$sql = "select sum(mk.sks) as sks_krs from registrasi, mk where nrp = :reg_uname AND mk.kodemk = registrasi.kodemk ";
					$stmt2 = $pdo->prepare($sql);
					$stmt2->execute(array(':reg_uname' => $_nrp )) or die("ERROR: " . implode(":", $stmt2->errorInfo()));  //reg jurusan dari protect1.php
					$_data2 = $stmt2->fetch(PDO::FETCH_OBJ);
					//list($sks_krs) = $row;

					/*
						mengubah disini YULIUS H
						untuk siswa angkatan baru agar tidak 12 SKS
					*/
					$limit_sks = $function->ips2sks($_data->ips, (substr($_tahun_ajaran,2,2) == $_nrp[3].$_nrp[4] ? true : false) );
					
					// Sampai disini saja!

					$sql = "SELECT nama FROM biodata WHERE nrp = :reg_uname ";
					$stmt3 = $pdo->prepare($sql);
					$stmt3->execute(array(':reg_uname' => $_nrp )) or die("ERROR: " . implode(":", $stmt->errorInfo())); //reg jurusan dari protect1.php
					$_data3 = $stmt3->fetch(PDO::FETCH_OBJ);
					?>
					<table class="data foot">
						<tbody>
							<tr>
							<tr><th width="25%">Nilai IPS terakhir </td><td>&nbsp;<?php echo $_data->ips; ?></td></tr>
							<tr><th>Limit SKS anda</td><td>&nbsp;<?php echo $limit_sks; ?> SKS</td></tr>
							<tr><th>Toleransi SKS anda</td><td>&nbsp;<?php echo $_data->toleransi; ?> SKS</td></tr>
							<tr><th>Sisa Limit SKS anda</td><td>&nbsp;<?php echo $limit_sks + $toleransi - $_data2->sks_krs; ?> SKS</td></tr>
						</tbody>
					</table>
					<div style="padding-bottom:50px;"></div>
				</div>
			</td>
		</tr>
	</table>
	<footer id="main">
	  <div><a href="http://www.widyakartika.ac.id/perpustakaan">Perpustakaan Online</a> | <a href="http://krs.widyakartika.ac.id/">KRS Online</a></div>
	  <div>Copyright &copy; 2012. <a href="http://www.widyakartika.ac.id">Universitas Widya Kartika</a> & <a href="#">Labkom ICT</a> - All rights reserved</div>
		<?php
			$_finish_timer = get_microtime();
			echo 'Page generated in '.round(($_finish_timer - $_start_timer), 4).' seconds.';
		?>
	</footer>
</body>
